var app = getApp();
var server = require('../../utils/server');
// 引入SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;


Page({
	data: {
    img_url: 'https://17fx.com/imgs/',
		filterId: 1,
		address: '定位中…',
    list1:[],
    list3:[],
    lblist:[],
		shops: app.globalData.shops,
    dingwei:['株洲','长沙','湘潭'],
    index:0,
    scrolltop:0,
    userinfo:null,
    openid1:null,
    list:[],
    plist:[],
    latitude:0,
    longitude:0,
    page:1,
    subscribe:1
	},
	onLoad: function () {

    var that = this;
    //创建一个dialog
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000
    });
    //调用商品类别接口
    Getglist2(that, 0)
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: '32OBZ-5X7WF-MRKJW-NACZJ-DVTIH-XLB6D'
    });
    
    //登录调用
    getApp().getUserInfo(function (userInfo, openid1) {
      console.log('登录。。。')
      //更新数据
      that.setData({
        userInfo: userInfo,
        openid: openid1,
        subscribe: getApp().globalData.subscribe
      })
      
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          console.log('经度' + res.latitude)
          console.log('维度' + res.longitude)

          that.setData({
            latitude: res.latitude,
            longitude: res.longitude
          })
          // 调用接口
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: that.data.latitude,
              longitude: that.data.longitude
            },
            success: function (res) {
              //城市
              var city = res.result.address_component.city
              //定位查询city_id ，把city_id写入user表中
              wx.request({
                url: 'https://17fx.com/weapp/api/getcity.html',
                data: {
                  city_name: city,
                  openid: getApp().globalData.openid
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  console.log(res.data);
                  getApp().globalData.city_id = res.data.city_id,//把定位的city_id保存到全局变量中
                    console.log(res.data.city_id);
                  //调用查询集合
                  Getglist(that, res.data.city_id, 1)//推荐商家查询
                  Getglist1(that, res.data.city_id)//首页轮播图

                  //获取城市集合
                  wx.request({
                    url: 'https://17fx.com/weapp/api/getcitylist.html',
                    data: {

                    },
                    header: {
                      'content-type': 'application/json'
                    },
                    success: function (res) {
                      console.log('=====================');
                      console.log(res.data);
                      //定位的城市排数组第一个
                      var list1 = new Array();
                      var list2 = new Array();

                      for (var i = 0; i < res.data.length; i++) {
                        if (res.data[i].city_id == getApp().globalData.city_id){
                          list1.push(res.data[i]);
                        }else{
                          list2.push(res.data[i]);
                        }
                      }
                      for (var i = 0; i < list2.length; i++) {
                        list1.push(list2[i]);
                      }
                      for (var i = 0; i < list1.length; i++) {
                        list1[i].cid = i;
                      }
                      
                      that.setData({
                        list1:list1,
                      })
                      wx.hideToast();
                    }
                  })



                }
              })

              

            },
            fail: function (res) {
              console.log(res);
            },
            complete: function (res) {
            }
          });
        }
      })
    })


    
	},
	onShow: function () {
    var that = this


	},
  //返回顶部
  scrolltop: function () {
    this.setData({
      scrolltop: 0
    })
  },
	onScroll: function (e) {
		if (e.detail.scrollTop > 100 && !this.data.scrollDown) {
			this.setData({
				scrollDown: true
			});
		} else if (e.detail.scrollTop < 100 && this.data.scrollDown) {
			this.setData({
				scrollDown: false
			});
		}
	},
  //搜索
	tapSearch: function () {
		wx.navigateTo({ url: '../search/search' });
	},
  //商家类别
	toNearby: function (e) {
		var id = e.currentTarget.dataset.id;
    console.log('商家类别' + e.currentTarget.dataset.id);
		// wx.setStorage({
		// 	data: id
		// })
    var url = '../shop/shop?id=' + e.currentTarget.dataset.id;
		wx.navigateTo({
			url: url,
		})
	},

  //定位城市，点击城市下拉菜单
  bindPickerChange:function(e){
    this.setData({
      index: e.detail.value
    }) 
    console.log('定位'+e.detail.value);

    var that = this;
    var list1 = that.data.list1;
    for (var i = 0; i < list1.length; i++) {
      list1[i].cid = i;
      if(list1[i].cid == e.detail.value){
        getApp().globalData.city_id = list1[i].city_id;
        //清空list的数据
        that.setData({
          list: [],
          page: 1
        })
        Getglist(that, list1[i].city_id, 1)
        Getglist1(that, list1[i].city_id)

        //把city_id写入数据库
        wx.request({
          url: 'https://17fx.com/weapp/api/setusercityid.html',
          data: {
            city_id: list1[i].city_id,
            openid: getApp().globalData.openid
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data);

          }
        })
      }
    }

    
  },

  //分享
  onShareAppMessage: function () {
    return {
      title: '17分享商城',
      desc: '最具人气的分享商城!',
      path: '/pages/index/index'
    }
  },
  //上拉加载数据绑定
  onReachBottom: function () {
    console.log("上拉加载数据绑定")
    var that = this
    //调用getlist方法加载排行集合
    Getglist(that, getApp().globalData.city_id,that.data.page)
  },
    //下拉刷新
  onPullDownRefresh: function () {
    //调用getlist方法获取美女排行榜集合
    var that = this
    console.log("下拉刷新数据绑定");
    //调用查询集合
    Getglist(that, getApp().globalData.city_id, 1)//推荐商家查询
    Getglist1(that, getApp().globalData.city_id)//首页轮播图
   
  },
  //跳转到关注页面
  guanzhupage:function(){
    wx.navigateTo({ url: '../index/guanzhu' });
  },

  //跳转到detail页面
  showtodetail:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({ url: '../detail/detail?id='+id });

  }
  

});

//
var url = "https://17fx.com/weapp/api/getshoplist.html";
//

var page_size = 10;
var Getglist = function (that,city_id,page) {
  wx.request({
    url: url,
    data: {
      openid: getApp().globalData.openid,
      city_id:city_id,
      page:page,
      page_size:page_size
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data);
      if (page == 1) {
        that.setData({
          list: []
        })
      }
      if(res.data != null){
        var list = that.data.list;
        for (var i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
        }
        that.setData({
          list: list,
          page: that.data.page + 1
        });
      }


    }
  })
}


var url1 = "https://17fx.com/weapp/api/getindexphoto.html";
//首页轮播
var Getglist1 = function (that, city_id) {
  wx.request({
    url: url1,
    data: {
      openid: getApp().globalData.openid,
      city_id: city_id
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log('轮播值'+res.data);
      if(res.data==0){
        that.setData({
          plist: null
        })
      }else{
        that.setData({
          plist: res.data
        })
      }
      


    }
  })
}

var url2 = "https://17fx.com/weapp/api/getgoods_category.html";
//首页类别
var Getglist2 = function (that, city_id) {
  wx.request({
    url: url2,
    data: {
      openid: getApp().globalData.openid,
      city_id: city_id
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data)
        that.setData({
          lblist: res.data
        })
    }
  })
}